<%@ page import="Dao.MessageDaoImpt" %>
<%@ page import="object.Message" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加消息中。。。</title>
    <!--检测是否非法访问-->
    <%
        Cookie cookie[] = request.getCookies();
        String uname = null;
        String uid = null;
        for (int i = 0; i < cookie.length; i++) {
            if (cookie[i].getName().equals("uname")) {
                uname = cookie[i].getValue();
            }
            if (cookie[i].getName().equals("uid")) {
                uid = cookie[i].getValue();
            }
        }
        if (uname == null || uname.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
        if (uid == null || uid.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
    %>
</head>
<body>
<%
    MessageDaoImpt dao = new MessageDaoImpt();
    Message message = new Message();
    message.updateTitle(request.getParameter("add-title"));
    message.updateAuthor(request.getParameter("add-uname"));
    message.updateContent(request.getParameter("add-content"));
    if (message.getContent() == null || message.getContent().equals("")) {
        out.println("<script>alert('内容为空无法添加，点击确认返回留言页面')</script>");
        response.setHeader("refresh", "0;URL=message.jsp");
    } else {
        Cookie cookie1 = new Cookie("uname", request.getParameter("add-uname"));
        response.addCookie(cookie1);
        int flag = dao.AddMessage(message);
        if (flag == 1) {
            out.println("<script>alert('添加成功，点击确认返回留言页面')</script>");
        } else if (flag == 2) {
            out.println("<script>alert('内容原因失败，点击确认返回留言页面')</script>");
        } else {
            out.println("<script>alert('特殊原因失败，请联系管理员，点击确认返回留言页面')</script>");
        }
        response.setHeader("refresh", "0;URL=message.jsp");
    }
%>
</body>
</html>
